//
//  NewManViewController.m
//  DZPoker
//
//  Created by Echo on 13-10-29.
//  Copyright (c) 2013年 Echo. All rights reserved.
//

#import "NewManViewController.h"
#import "ComBtn.h"
#import "AppDelegate.h"
#import "singlePeripheral.h"
#import "WsHUD.h"
#import "CircularProgressView.h"
#import "CCProgressView.h"
#import "RecInfo.h"
#import "FDGraphView.h"
#import "TodayViewController.h"
#import "HistoryTableViewController.h"

@interface NewManViewController (){
    CircularProgressView *circularProgressView;
    CCProgressView *_circleChart;
    
}

@end

@implementation NewManViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        maxCapability = [singlePeripheral calWaiterNum];
        [NC addObserver:self selector:@selector(endSyn) name:NOTI_END_SYN_DATA object:nil];
        [NC addObserver:self selector:@selector(lastWaterNum:) name:ICUP_LAST_WATER_NUM object:nil];
    }
    return self;
}
-(void)lastWaterNum:(NSNotification*)noti{
//    NSArray *array = noti.object;
//    if (array.count > 1){
//        NSNumber *ml = [array objectAtIndex:0];
//        [UD setObject:ml forKey:ICUP_CUR_WAITER_NUM];
//        [UD synchronize];
//        [self setProgressCircle:[ml floatValue]];
//    }
    [self setTodayWaterMeasure];
    
    [self setScrollViewData];
    
}
-(void)setTodayWaterMeasure{
    [self setProgressCircle:[[singlePeripheral instance] getTodayWaterMeasure]];
}

-(void)endSynNext{
    label4.hidden = YES;
}

-(void)endSyn{
    label4.text = @"同步完成";
    [self performSelector:@selector(endSynNext) withObject:nil afterDelay:3];
}
-(void)sendDataToBluetooth:(NSString*)str{
    [[singlePeripheral instance] sendDataToBluetooth:str];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = RGB(42, 47, 56, 1);
    
//    if ([singlePeripheral instance].isFindDeviceing) {
//        [WsHUD showHUDWithLabel:STR(@"请稍候...") modal:YES timeoutDuration:0];
//    }
    
    ComBtn *leftBtn = [ComBtn btnWithImg:[UIImage imageNamed:@"home_push_menu.png"]];
    leftBtn.frame = CGRectMake(5, 22, 76/2, 76/2);
    [leftBtn addTarget:[AppDelegate instance].rootViewController action:@selector(showLeftView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftBtn];
    
    UIButton *AdView = [[UIButton alloc] initWithFrame:RECT((SCREEN_WIDTH-120)/2, 25, 120, 44)];
    [AdView setBackgroundImage:IMG(@"今天 底色 改.png") forState:UIControlStateNormal];
    [AdView setTitle:@"今天" forState:UIControlStateNormal];
    [AdView addTarget:self action:@selector(AdViewPressed) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:AdView];
    
    NSDateFormatter *formatter	=  [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy/MM/dd"];//HH:mm
    NSString  *strdate = [formatter stringFromDate:[NSDate date]];
    NSLog(@"strdate %@", strdate);
    
    label3 = [[UILabel alloc] initWithFrame:CGRectMake(0, FY(AdView)+FH(AdView), SCREEN_WIDTH, 50)];
    label3.textColor = RGB(37, 190, 187, 1);
    label3.textAlignment = UITextAlignmentCenter;
    label3.font = [UIFont systemFontOfSize:17];
    label3.backgroundColor = [UIColor clearColor];
    [self.view addSubview:label3];
    
    
    UIImageView *imageviewbg = [[UIImageView alloc] initWithImage:IMG(@"外圈-.png")];
    imageviewbg.userInteractionEnabled = YES;
    imageviewbg.frame = CGRectMake((SCREEN_WIDTH-FW(imageviewbg))/2, FY(label3)+40, FW(imageviewbg), FH(imageviewbg));
    [self.view addSubview:imageviewbg];
    
    circularProgressView = [[CircularProgressView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-FW(imageviewbg))/2, FY(imageviewbg), FW(imageviewbg), FH(imageviewbg))];
    circularProgressView.userInteractionEnabled = NO;
    circularProgressView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:circularProgressView];
    circularProgressView.backColor = [UIColor colorWithRed:121 / 255.0
                                                     green:127 / 255.0
                                                      blue:125 / 255.0
                                                     alpha:1.0];
    circularProgressView.progressColor = [UIColor colorWithRed:50 / 255.0
                                                         green:113 / 255.0
                                                          blue:242 / 255.0
                                                         alpha:1.0];
    
    
    circularProgressView.lineWidth = 5;
    
    [circularProgressView updateProgressCircle:.5];
    
    UIButton *imageview = [[UIButton alloc] init];
    [imageview addTarget:self action:@selector(aynPressed) forControlEvents:UIControlEventTouchUpInside];
    [imageview setBackgroundImage:IMG(@"内圈-背景.png") forState:UIControlStateNormal];
    imageview.frame = CGRectMake((FW(imageviewbg)-302/2)/2, (FH(imageviewbg)-302/2)/2, 302/2, 302/2);
    [imageviewbg addSubview:imageview];
    
//    UIImageView *imageview1 = [[UIImageView alloc] initWithImage:IMG(@"水量 波纹~.png")];
//    imageview1.frame = CGRectMake((FW(imageview)-FW(imageview1))/2, FH(imageview)-FH(imageview1), FW(imageview1), FH(imageview1));
//    [imageview addSubview:imageview1];
    //43 73 92
    _circleChart = [[CCProgressView alloc] initWithFrame:CGRectMake(10, 10, 302/2,302/2)];
    _circleChart.userInteractionEnabled = NO;
    _circleChart.backgroundColor = RGB(43, 73, 92, 1);
    [imageviewbg addSubview:_circleChart];
    
    
    label4 = [[UILabel alloc] initWithFrame:CGRectMake(0, FY(label3)+FH(label3)+50, SCREEN_WIDTH, 50)];
    label4.textColor = [UIColor grayColor];
    label4.textAlignment = UITextAlignmentCenter;
    label4.font = [UIFont systemFontOfSize:17];
    label4.backgroundColor = [UIColor clearColor];
    [self.view addSubview:label4];
    label4.text = @"正在同步数据...";
    label4.hidden = YES;
    
    if ([singlePeripheral instance].isSyning) {
        label4.hidden = NO;
    }
    
    for (int i=0; i<2; i++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-380/2, SCREEN_WIDTH, 40)];
        label.textColor = [UIColor whiteColor];
        label.textAlignment = UITextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:50];
        label.backgroundColor = [UIColor clearColor];
        [self.view addSubview:label];
        if (i == 0) {
            label5 = label;
        }else{
            label6 = label;
            label.font = [UIFont systemFontOfSize:17];
            label.center = CGPointMake(label.center.x+40, label.center.y+10);
            label6.text = @"ml";
        }
    }

    [self setTodayWaterMeasure];
    
    [self setScrollViewData];
}

-(void)setScrollViewData{
    int count = 10;
    
    [scrollView removeFromSuperview];
    scrollView = [[UIScrollView alloc] init];
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.backgroundColor = [UIColor whiteColor];
    scrollView.frame = CGRectMake(0, SCREEN_HEIGHT-120, SCREEN_WIDTH, 120);
    [self.view addSubview:scrollView];
    
    
    float columnHeight = 100;
    float columnWidh = 50;
    float columnX = 10;
    NSMutableArray *_sectionIndexTitles = [[NSMutableArray alloc] init];
    NSArray *nsArry = [[[singlePeripheral instance].todayDic allKeys] sortedArrayUsingSelector:@selector(compare:)];
    for (NSNumber *time in nsArry) {
        NSNumber *ml = [[singlePeripheral instance].todayDic objectForKey:time];
        if ([ml integerValue] > 0) {
            [_sectionIndexTitles addObject:time];
        }
    }
    count = _sectionIndexTitles.count;
    
    NSInteger maxCapacity = 300;
    for (int i=0; i<_sectionIndexTitles.count; i++) {
        NSString *capacity = [[singlePeripheral instance].todayDic objectForKey:[_sectionIndexTitles objectAtIndex:i]];
        if (maxCapacity < [capacity integerValue]) {
            maxCapacity = [capacity integerValue];
        }
    }
    
    
    NSMutableArray *numArray = [NSMutableArray array];
    
    for (int i=0; i<_sectionIndexTitles.count; i++) {
        
        UIView *h_view = [[UIView alloc] initWithFrame:CGRectMake(columnX+i*columnWidh, FH(scrollView)-10, columnWidh, 3)];
        h_view.backgroundColor = RGB(167, 179, 187, 1);
        UIView *v_view = [[UIView alloc] initWithFrame:CGRectMake(FX(h_view), FY(h_view)-columnHeight, 2, columnHeight)];
        v_view.backgroundColor = RGB(212, 218, 230, 1);
        float v_imgview_h = columnHeight - 19;
        UIImageView *v_imgview = [[UIImageView alloc] initWithFrame:CGRectMake(FX(v_view)+columnWidh/2.0, FY(h_view)-v_imgview_h, 2, v_imgview_h)];
        v_imgview.image = IMG(@"water_v_line.png");
        [scrollView addSubview:h_view];
        [scrollView addSubview:v_view];
        [scrollView addSubview:v_imgview];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(FX(v_view)+FW(v_view), FY(v_view)-5, 50, 20)];
        label.text = [NSString stringWithFormat:@"%@:00", [_sectionIndexTitles objectAtIndex:i]];
        label.font = [UIFont systemFontOfSize:10];
        label.backgroundColor = [UIColor clearColor];
        [scrollView addSubview:label];
        
        label = [[UILabel alloc] initWithFrame:CGRectMake(FX(v_view)+FW(v_view), FY(v_view)+25, 50, 20)];
        label.textColor = RGB(252, 139, 87, 1);
        label.text = [NSString stringWithFormat:@"%dml", [[[singlePeripheral instance].todayDic objectForKey:[_sectionIndexTitles objectAtIndex:i]] integerValue]*5];
        label.font = [UIFont systemFontOfSize:10];
        label.backgroundColor = [UIColor clearColor];
        [scrollView addSubview:label];
        
        NSString *capacity = [[singlePeripheral instance].todayDic objectForKey:[_sectionIndexTitles objectAtIndex:i]];
        NSInteger percent = FH(scrollView)*[capacity integerValue]/maxCapacity;
        [numArray addObject:[NSNumber numberWithInteger:percent]];
    }
    
    scrollView.contentSize = CGSizeMake(columnX+columnWidh*count, FH(scrollView));
    
    FDGraphView *graphView = [[FDGraphView alloc] initWithFrame:CGRectMake(1, 15, FW(scrollView), FH(scrollView)-30)];
    if (numArray.count == 1) {
        graphView.frame = CGRectMake(-14, 15, FW(scrollView), FH(scrollView)-30);
    }
    graphView.backgroundColor = [UIColor clearColor];
    graphView.autoresizeToFitData = YES;
    [scrollView addSubview:graphView];
    graphView.dataPoints = numArray;
}
-(void)setProgressCircle:(float)pro{
    [circularProgressView updateProgressCircle:pro/maxCapability];
    int num = pro*100.0/maxCapability;
    if (num == 0) {
        num = 1;
    }
    if (pro == 0) {
         num = 0;
    }
    label3.text =[NSString stringWithFormat:@"%d%@", num,  @"%"];
    float fnum = pro/maxCapability;
    [_circleChart setProgress:fnum animated:YES];
    
    label5.text = [NSString stringWithFormat:@"%d", (int)pro*5];
    if ([label5.text integerValue] > 99) {
        label6.frame = CGRectMake(40+10, FY(label6), SCREEN_WIDTH, FH(label6));
    }else if ([label5.text integerValue] > 999) {
        label6.frame = CGRectMake(40+20, FY(label6), SCREEN_WIDTH, FH(label6));
    }
    
}
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
-(void)aynPressed{
    label4.hidden = NO;
    label4.text = @"正在同步数据...";
    if (![singlePeripheral instance].isSyning) {
        [self sendDataToBluetooth:@"324345bb5533"];
    }
}
//-(void)startScanPeripheral{
//    [singlePeripheral instance];
//}
-(void)AdViewPressed{
    TodayViewController *view = [[TodayViewController alloc] init];
    [self.navigationController pushViewController:view animated:YES];
    
//    HistoryTableViewController *view = [[HistoryTableViewController alloc] init];
//    [self.navigationController pushViewController:view animated:YES];
}

-(void)releaseResources{
    
}

-(void)dealloc {
    [NC removeObserver:self];
}
@end





